<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class kuesioner extends arfaController {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('m_pendidikan_model');
		$this->load->model('m_pekerjaan_model');
		$this->load->model('m_usia_model');
		$this->load->model('m_urus_izin_model');
		$this->load->model('m_kuesioner_model');
		$this->load->model('m_ijin_model');
		$this->load->model('m_tanya_model');
		$this->load->model('dt_tanya_model');
		$this->load->model('m_kuesioner_jawaban_model');
		$this->load->model('m_kritik_saran_model');
	}

	// responden
	public function index()
	{
		$this->form_validation->set_rules('jk', 'Jenis Kelamin', 'required');
		$this->form_validation->set_rules('usia', 'Usia', 'required');
		$this->form_validation->set_rules('pekerjaan', 'Pekerjaan', 'required');
		$this->form_validation->set_rules('pendidikan', 'Pendidikan Terakhir', 'required');
		$this->form_validation->set_rules('urus_izin', 'Frekuensi Urus Izin', 'required');

		if( $this->form_validation->run() == FALSE ) {
			$data["usia"] 			= $this->m_usia_model->get_key_usia_aktif();
			$data["pekerjaan"] 		= $this->m_pekerjaan_model->get_key_pekerjaan_aktif();
			$data["pendidikan"] 	= $this->m_pendidikan_model->get_key_pendidikan_aktif();
			$data["urus_izin"] 		= $this->m_urus_izin_model->get_key_urus_izin_aktif();
			$data["content"] 		= $this->load->view("form_kuesioner_responden",$data,true);
			$this->load->view('main2',$data);
		} else {
			$jk 		= $this->input->post("jk");
			$usia 		= $this->input->post("usia");
			$pendidikan = $this->input->post("pendidikan");
			$pekerjaan 	= $this->input->post("pekerjaan");
			$urus_izin 	= $this->input->post("urus_izin");

			$data = array(
				'jenis_kelamin' => $jk[0],
				'kd_usia' => $usia[0],
				'kd_pendidikan' => $pendidikan[0],
				'kd_pekerjaan' => $pekerjaan[0],
				'kd_urus_izin' => $urus_izin[0],
				'TGL_KUESIONER'=>date("Y-m-d")
			);

			$this->m_kuesioner_model->save($data);

			$count = count( $this->m_kuesioner_model->get() );

			redirect('kuesioner/jenis_izin/'.$count);
		}
	}
	// end responden

	// jenis izin
	public function jenis_izin($parameter)
	{
		$this->form_validation->set_rules("jenis_izin", "Jenis Izin", "required");

		if ( $this->form_validation->run() == FALSE ) {
			$jenis_izin = array(""=>"-- Pilih Jenis Izin --");
			$jenis_izin += $this->m_ijin_model->get_key_jenis_izin('ID_IJIN','NAMA_IJIN');
			$data['jenis_izin'] = $jenis_izin;
			$data["menu"] 		= "responden";
			$data["parameter"]	= $parameter;
			$data["content"] 	= $this->load->view("form_kuesioner_jenis_izin",$data,true);
			$this->load->view('main2',$data);
		} else {
			$jenis_izin = $this->input->post("jenis_izin");
			$data = array("ID_IJIN" => $jenis_izin);
			$this->m_kuesioner_model->save($data,$parameter);
			redirect("kuesioner/pertanyaan/".$parameter."/".$jenis_izin."/1");
		}
	}
	// end jenis izin

	// pertanyaan
	public function pertanyaan($parameter, $jenis_izin, $urutan)
	{
		$query = $this->dt_tanya_model->dt_pertanyaan_join_master_pertanyaan($jenis_izin);
		$dummy = count($query);
		// dump($query);

		if ($urutan < $dummy) {
			$this->form_validation->set_rules("jawaban", "Jawaban", "required");
			$counter = $urutan - 1;
			$id_pertanyaan = $query[$counter]["ID_PERTANYAAN"];

			if( $this->form_validation->run() == FALSE ) {
				$query_dua = $this->m_kuesioner_jawaban_model->kuesioner_join_kuesioner_jawaban($parameter, $id_pertanyaan);
				if( $query_dua != NULL ) {
					// edit -> checkbox ke-check
					$data["ID_PERTANYAAN"] 	= $query[$counter]["ID_PERTANYAAN"];
					$data["URUTAN"] 		= $urutan;
					$data["PERTANYAAN"] 	= $query[$counter]["PERTANYAAN"];
					$data["JAWABAN"] 		= $query[$counter]["JAWABAN"];
					$data["JAWABAN2"] 		= $query[$counter]["JAWABAN2"];
					$data["JAWABAN3"] 		= $query[$counter]["JAWABAN3"];
					$data["JAWABAN4"] 		= $query[$counter]["JAWABAN4"];
					$data["CHECKED"]		= $query_dua[0]["nilai"];
				} else {
					// save -> kosongan
					$data["ID_PERTANYAAN"] 	= $query[$counter]["ID_PERTANYAAN"];
					$data["URUTAN"] 		= $urutan;
					$data["PERTANYAAN"] 	= $query[$counter]["PERTANYAAN"];
					$data["JAWABAN"] 		= $query[$counter]["JAWABAN"];
					$data["JAWABAN2"] 		= $query[$counter]["JAWABAN2"];
					$data["JAWABAN3"] 		= $query[$counter]["JAWABAN3"];
					$data["JAWABAN4"] 		= $query[$counter]["JAWABAN4"];
				}
				$data["menu"] 		= "responden";
				$data["parameter"]	= $parameter;
				$data["jenis_izin"]	= $jenis_izin;
				$data["content"] 	= $this->load->view("form_kuesioner_pertanyaan",$data,true);
				$this->load->view('main2',$data);
			} else {
				$jawaban 	= $this->input->post('jawaban');
				$query_dua 	= $this->m_kuesioner_jawaban_model->kuesioner_join_kuesioner_jawaban($parameter, $id_pertanyaan);
				if( $query_dua != NULL ) {
					// edit
					// $id	= $query_dua[0]["kd_kuesioner_jawaban"];
					$data = array(
						"kd_kuesioner" => $parameter,
						"ID_PERTANYAAN" => $id_pertanyaan,
						"nilai" => $jawaban[0]
					);
					// $this->m_kuesioner_jawaban_model->save($data,$id);
					$this->m_kuesioner_jawaban_model->save($data,array("kd_kuesioner" => $parameter, "ID_PERTANYAAN" => $id_pertanyaan));
				} else {
					// save
					$data = array(
						"kd_kuesioner" => $parameter,
						"ID_PERTANYAAN" => $id_pertanyaan,
						"nilai" => $jawaban[0]
					);
					$this->m_kuesioner_jawaban_model->save($data);
				}
				$urutan_next = $urutan + 1;
				redirect(site_url().'/kuesioner/pertanyaan/'.$parameter.'/'.$jenis_izin.'/'.$urutan_next);
			}
		} else {
			$this->form_validation->set_rules("jawaban", "Jawaban", "required");
			$counter = $urutan - 1;
			$id_pertanyaan = $query[$counter]["ID_PERTANYAAN"];

			if( $this->form_validation->run() == FALSE ) {
				$query_dua = $this->m_kuesioner_jawaban_model->kuesioner_join_kuesioner_jawaban($parameter, $id_pertanyaan);
				if( $query_dua != NULL ) {
					// edit -> checkbox ke-check
					$data["ID_PERTANYAAN"] 	= $query[$counter]["ID_PERTANYAAN"];
					$data["URUTAN"] 		= $urutan;
					$data["PERTANYAAN"] 	= $query[$counter]["PERTANYAAN"];
					$data["JAWABAN"] 		= $query[$counter]["JAWABAN"];
					$data["JAWABAN2"] 		= $query[$counter]["JAWABAN2"];
					$data["JAWABAN3"] 		= $query[$counter]["JAWABAN3"];
					$data["JAWABAN4"] 		= $query[$counter]["JAWABAN4"];
					$data["CHECKED"]		= $query_dua[0]["nilai"];
					$data["MAKSIMAL"]		= "maksimal";
				} else {
					// save -> kosongan
					$data["ID_PERTANYAAN"] 	= $query[$counter]["ID_PERTANYAAN"];
					$data["URUTAN"] 		= $urutan;
					$data["PERTANYAAN"] 	= $query[$counter]["PERTANYAAN"];
					$data["JAWABAN"] 		= $query[$counter]["JAWABAN"];
					$data["JAWABAN2"] 		= $query[$counter]["JAWABAN2"];
					$data["JAWABAN3"] 		= $query[$counter]["JAWABAN3"];
					$data["JAWABAN4"] 		= $query[$counter]["JAWABAN4"];
					$data["MAKSIMAL"]		= "maksimal";
				}
				$data["menu"] 		= "responden";
				$data["parameter"]	= $parameter;
				$data["jenis_izin"]	= $jenis_izin;
				$data["content"] 	= $this->load->view("form_kuesioner_pertanyaan",$data,true);
				$this->load->view('main2',$data);
			} else {
				$jawaban 	= $this->input->post('jawaban');
				$query_dua 	= $this->m_kuesioner_jawaban_model->kuesioner_join_kuesioner_jawaban($parameter, $id_pertanyaan);
				if( $query_dua != NULL ) {
					// edit
					// $id = $query_dua[0]["kd_kuesioner_jawaban"];
					$data = array(
						"kd_kuesioner" => $parameter,
						"ID_PERTANYAAN" => $id_pertanyaan,
						"nilai" => $jawaban[0]
					);
					// $this->m_kuesioner_jawaban_model->save($data,$id);
					$this->m_kuesioner_jawaban_model->save($data,array("kd_kuesioner" => $parameter, "ID_PERTANYAAN" => $id_pertanyaan));
				} else {
					// save
					$data = array(
						"kd_kuesioner" => $parameter,
						"ID_PERTANYAAN" => $id_pertanyaan,
						"nilai" => $jawaban[0]
					);
					$this->m_kuesioner_jawaban_model->save($data);
				}
				redirect(site_url("kuesioner/kritik_saran/".$parameter));
			}
		}
	}
	// end pertanyaan

	// kritik dan saran
	public function kritik_saran($parameter)
	{
		$this->form_validation->set_rules("modul", "Modul", "required");
		$this->form_validation->set_rules("isian", "Isian", "required");

		$query_satu = $this->m_kritik_saran_model->get_by(array('kd_kuesioner' => $parameter, 'kd_modul' => 'Biaya'));
		if ( isset($query_satu) == "" || isset($query_satu[0]) ) {
			$array_satu = array();
		} else {
			$array_satu = array("Biaya" => "Biaya");
		}

		$query_dua = $this->m_kritik_saran_model->get_by(array('kd_kuesioner' => $parameter, 'kd_modul' => 'SDM'));
		if ( isset($query_dua) == "" || isset($query_dua[0]) ) {
			$array_dua = array();
		} else {
			$array_dua = array("SDM" => "SDM");
		}

		$query_tiga = $this->m_kritik_saran_model->get_by(array('kd_kuesioner' => $parameter, 'kd_modul' => 'Waktu Penyelesaian'));
		if ( isset($query_tiga) == "" || isset($query_tiga[0]) ) {
			$array_tiga = array();
		} else {
			$array_tiga = array("Waktu Penyelesaian" => "Waktu Penyelesaian");
		}

		$query_empat = $this->m_kritik_saran_model->get_by(array('kd_kuesioner' => $parameter, 'kd_modul' => 'Metode - Prosedur'));
		if ( isset($query_empat) == "" || isset($query_empat[0]) ) {
			$array_empat = array();
		} else {
			$array_empat = array("Metode - Prosedur" => "Metode - Prosedur");
		}

		$query_lima = $this->m_kritik_saran_model->get_by(array('kd_kuesioner' => $parameter, 'kd_modul' => 'Dokumen Persyaratan'));
		if ( isset($query_lima) == "" || isset($query_lima[0]) ) {
			$array_lima = array();
		} else {
			$array_lima = array("Dokumen Persyaratan" => "Dokumen Persyaratan");
		}

		$query_enam = $this->m_kritik_saran_model->get_by(array('kd_kuesioner' => $parameter, 'kd_modul' => 'Sarana - Prasarana'));
		if ( isset($query_enam) == "" || isset($query_enam[0]) ) {
			$array_enam = array();
		} else {
			$array_enam = array("Sarana - Prasarana" => "Sarana - Prasarana");
		}

		$array_dummy = array("0" => "-- Pilih Salah Satu Modul Untuk Mengisi Kritik dan Saran --");

		if ( $this->form_validation->run() == FALSE ) {
			$data["modul"] = array_merge($array_dummy,$array_satu,$array_dua,$array_tiga,$array_empat,$array_lima,$array_enam);
			$data["menu"] 		= "responden";
			$data["parameter"]	= $parameter;
			$data["content"] 	= $this->load->view("form_kuesioner_kritik_saran",$data,true);
			$this->load->view('main2',$data);
		} else {
			$modul = $this->input->post('modul');
			$isian = $this->input->post('isian');

			$data = array('kd_kuesioner' => $parameter, 'kd_modul' => $modul, 'kritik_saran' =>$isian);
			$this->m_kritik_saran_model->save($data);

			redirect(site_url("kuesioner/kritik_saran/".$parameter));
		}
	}
	// end kritik dan saran

	// start report global setahun
	public function r_global_setahun() {
		$tgl_awal = date("Y-01-01");
		$tgl_akhir = date("Y-m-d");
		$data["menu"] 	= "responden";
		$data["query"]	= $this->m_kuesioner_model->get_chart($tgl_awal,$tgl_akhir);
		$data["query3"]	= $this->m_kuesioner_model->get_chart_usia($tgl_awal,$tgl_akhir);
		$data["query4"]	= $this->m_kuesioner_model->get_chart_perkerjaan($tgl_awal,$tgl_akhir);
		$data["query5"]	= $this->m_kuesioner_model->get_chart_pendidikan($tgl_awal,$tgl_akhir);
		$data["query6"]	= $this->m_kuesioner_model->get_chart_izin($tgl_awal,$tgl_akhir);
		$data["query2"]	= $this->m_kuesioner_model->get_chart_bar($tgl_awal,$tgl_akhir);
		$data["content"]= $this->load->view("chart_r_global_setahun",$data,true);
		$this->load->view("main2",$data);
	}
	// end report global setahun

}